kotlin let in two variables

119

// Example usage:

val risk = safeLet(person.name, person.age) { name, age ->
  // do something
}

// Extension functions
inline fun <T1: Any, T2: Any, R: Any> safeLet(p1: T1?, p2: T2?, block: (T1, T2)->R?): R? {
    return if (p1 != null && p2 != null) block(p1, p2) else null
}
inline fun <T1: Any, T2: Any, T3: Any, R: Any> safeLet(p1: T1?, p2: T2?, p3: T3?, block: (T1, T2, T3)->R?): R? {
    return if (p1 != null && p2 != null && p3 != null) block(p1, p2, p3) else null
}
inline fun <T1: Any, T2: Any, T3: Any, T4: Any, R: Any> safeLet(p1: T1?, p2: T2?, p3: T3?, p4: T4?, block: (T1, T2, T3, T4)->R?): R? {
    return if (p1 != null && p2 != null && p3 != null && p4 != null) block(p1, p2, p3, p4) else null
}
inline fun <T1: Any, T2: Any, T3: Any, T4: Any, T5: Any, R: Any> safeLet(p1: T1?, p2: T2?, p3: T3?, p4: T4?, p5: T5?, block: (T1, T2, T3, T4, T5)->R?): R? {
    return if (p1 != null && p2 != null && p3 != null && p4 != null && p5 != null) block(p1, p2, p3, p4, p5) else null
}

// ...keep going up to the parameter count you care about
// Here are a few variations, depending on what style you will want to use, if you have everything of same or different types, and if the list unknown number of items...

Mixed types, all must not be null to calculate a new value
For mixed types you could build a series of functions for each parameter count that may look silly, but work nicely for mixed types:

inline fun <T1: Any, T2: Any, R: Any> safeLet(p1: T1?, p2: T2?, block: (T1, T2)->R?): R? {
    return if (p1 != null && p2 != null) block(p1, p2) else null
}
inline fun <T1: Any, T2: Any, T3: Any, R: Any> safeLet(p1: T1?, p2: T2?, p3: T3?, block: (T1, T2, T3)->R?): R? {
    return if (p1 != null && p2 != null && p3 != null) block(p1, p2, p3) else null
}
inline fun <T1: Any, T2: Any, T3: Any, T4: Any, R: Any> safeLet(p1: T1?, p2: T2?, p3: T3?, p4: T4?, block: (T1, T2, T3, T4)->R?): R? {
    return if (p1 != null && p2 != null && p3 != null && p4 != null) block(p1, p2, p3, p4) else null
}
inline fun <T1: Any, T2: Any, T3: Any, T4: Any, T5: Any, R: Any> safeLet(p1: T1?, p2: T2?, p3: T3?, p4: T4?, p5: T5?, block: (T1, T2, T3, T4, T5)->R?): R? {
    return if (p1 != null && p2 != null && p3 != null && p4 != null && p5 != null) block(p1, p2, p3, p4, p5) else null
}

// ...keep going up to the parameter count you care about

Comments

Submit
0 Comments

More Questions

kotlin date to stringtoast in kotlin zsh: command not found: adb
coroutines kotlin android dependency2d array in kotlin how to initialize mutable list kotlin
date format kotlinkotlin check if string contains android recyclerview scroll to bottom
on click in kotlinrandom string generator kotlin kotlin list add
kotlin onclicklistenerkotlin empty list kotlin filter not null
what is the difference between const and valkotlin print list text is behind BottomNavigationView
kotlinx coroutines dependencyhow to check internet connection in android programmatically kotlin recyclerview onclicklistener kotlin
kotlin check if edittext is emptycountdowntimer kotlin kotlin null safety
how to get date in kotlinkotlin repeat n times split string kotlin
Kotlin redundant FindViewByIdfile is not upload using retrofit in kotlin how to uppercase the first letter of a string in kotlin
how to make dialog transparent androidstartactivityforresult deprecated android kotlin remove name from an activity
button inside a recycle view in android in kotlindate to string kotlin while loop kotlin
kotlin variable possiblement nullregex find emails kotlin for decrement
kotlin filter map by keyadd kotlin parcelize plugin in new android studio list map by index and value kotlin
object vs companion object kotlinhow to get resource how to get data in mainActivity from a parcelable data class in kotlin
android navigation drawer not navigating with navcontrollerdrawable to bitmap android kotlin double tab exit button